﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddContact.aspx.cs" company="">
//
// </copyright>
// <summary>
//   The add contact test.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.AContact
{
    /// <summary>
    /// The add contact test.
    /// </summary>
    public partial class AddContactTest : Page
    {
        #region Methods

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Contact_AnhTT12/ContactList.aspx");
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            string firstName = TextBoxFirstName.Text;

            int? managerName = null;
            if (!string.IsNullOrEmpty(HiddenFieldManagerID.Value))
            {
                managerName = Convert.ToInt32(HiddenFieldManagerID.Value, CultureInfo.CurrentCulture);
            }

            string surname = TextBoxSurname.Text;
            int contactType = Convert.ToInt32(DropDownListContactType.SelectedValue, CultureInfo.CurrentCulture);
            string knowAs = TextBoxKnowAs.Text;
            int bestMethod = Convert.ToInt32(DropDownListBestMethod.SelectedValue, CultureInfo.CurrentCulture);
            string officePhone = TextBoxOfficePhone.Text;
            string mobilePhone = TextBoxMobilePhone.Text;
            string workBase = TextBoxWorkBase.Text;
            string homePhone = TextBoxHomePhone.Text;
            string jobTitle = TextBoxJobTitle.Text;
            string email = TextBoxEmailAddress.Text;
            string jobRole = TextBoxJobRole.Text;
            bool active;
            if (CheckBoxIsActive.Checked)
            {
                active = true;
            }
            else
            {
                active = false;
            }

            bool insertData =
                AContactBO.AddContact(
                    new Contact
                        {
                            FirstName = firstName,
                            BestContactMethodId = bestMethod,
                            ManageId = managerName,
                            Surname = surname,
                            ContactTypeId = contactType,
                            KnownAs = knowAs,
                            OfficePhone = officePhone,
                            MobilePhone = mobilePhone,
                            WorkBase = workBase,
                            HomePhone = homePhone,
                            JobRole = jobRole,
                            JobTitle = jobTitle,
                            Email = email,
                            IsActive = active
                        });
            if (insertData)
            {
                Response.Redirect("~/Contact_AnhTT12/ContactList.aspx");
            }
            else
            {
                Response.Write("<script>alert('Insert contact to database failed ! ')</script>");
            }
        }

        /// <summary>
        /// The contact object data source_ selecting.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void ContactObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as AContactFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Click event of the LinkButtonLookup control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        protected void LinkButtonLookup_Click(object sender, EventArgs e)
        {
            ContactLookupPopup.Popup();
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["filter"] = new AContactFilter();
                LoadAllBestContactMethod();
                LoadContactType();
            }
        }

        /// <summary>
        /// Loads all best contact method.
        /// </summary>
        private void LoadAllBestContactMethod()
        {
            ICollection<BestContactMethod> collection = ABestContactMethodBO.FindAllBestContactMethod();
            DropDownListBestMethod.DataSource = collection;
            DropDownListBestMethod.DataBind();
        }

        /// <summary>
        /// Loads the type of the contact.
        /// </summary>
        private void LoadContactType()
        {
            ICollection<ContactType> collection = AContactTypeBO.FindAllContactType();
            DropDownListContactType.DataSource = collection;
            DropDownListContactType.DataBind();
        }

        #endregion Methods
    }
}